package com.hfjyz.system.user.bean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.hfjyz.base.bean.BaseBean;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.entity.UserEntity;
import com.hfjyz.base.util.BaseUtil;

public class UserBean extends BaseBean {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -7196784471813585672L;
    private Integer id = null;
    private String userId = null;
    private String userName = null;
    private String password = null;
    private Integer departId = null;
    private String departName = null;
    private int sex = 1;
    private String birthday = null;

    public UserBean() {
    }

    public UserBean(UserEntity entity) {
        setId(entity.getId());
        setUserId(entity.getUserId());
        setUserName(entity.getUserName());
        setPassword(entity.getPassword());
        if (entity.getIsMale()) {
            setSex(1);
        } else {
            setSex(0);
        }
        setDepartId(entity.getDepart().getId());
        setDepartName(entity.getDepart().getDepartName());

        if (entity.getBirthday() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(BaseConstant.DATE_PATTERN_BAR);
            String date = sdf.format(entity.getBirthday());
            setBirthday(date);
        }
    }

    @Override
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getDepartId() {
        return departId;
    }

    public void setDepartId(Integer departId) {
        this.departId = departId;
    }

    public String getDepartName() {
        return departName;
    }

    public void setDepartName(String departName) {
        this.departName = departName;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Override
    public List<String> toJsonArray(int index) {
        List<String> result = new ArrayList<String>();
        result.add(userId);
        result.add(userName);
        if (sex == 1) {
            result.add("男");
        } else {
            result.add("女");
        }
        result.add(departName);
        result.add(birthday);

        return result;
    }

    @Override
    public String validate() {
        List<String> errorList = new ArrayList<String>();
        if (userId.isEmpty()) {
            errorList.add("用户ID必须输入");
        } else if (userId.length() != 6) {
            errorList.add("用户ID长度必须为6");
        } else if (!BaseUtil.isAlpha(userId)) {
            errorList.add("用户ID必须为半角英数");
        }
        if (userName.length() > 16) {
            errorList.add("用户名长度不能超过16");
        }
        if (!birthday.isEmpty()) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(BaseConstant.DATE_PATTERN_BAR);
                sdf.parse(birthday);
            } catch (Exception e) {
                errorList.add("日期格式或内容输入不正确");
            }
        }
        return getErrMsgForUI(errorList);
    }
}
